How To Set Environment Variables In Ubuntu

Ubuntu Sign in to your XDA account With laptops powered by Linux becoming commonplace, now’s the best time to dive right into the open-source OS if you’re into coding. That … Read more

Taylor Bell

Taylor Bell

Published on Jul 10, 2024

How To Set Environment Variables In Ubuntu
Ubuntu

4

4

Sign in to your XDA account

The commands used to configure environment variables in Ubuntu

With laptops powered by Linux becoming commonplace, now’s the best time to dive right into the open-source OS if you’re into coding. That said, switching to a Linux distro can be rather jarring as it does things quite differently than Windows or macOS. For instance, the procedure for setting up environment variables is a lot less intuitive on Linux than it is on Windows 11. So, we’ve compiled this guide to help you configure environment variables on your Ubuntu machine.

Ubuntu logo.

Related

What is Ubuntu? The ins and outs of one of the most popular Linux distros

When it comes to Linux, one of its most mainstream implementations is Ubuntu. Here’s everything you need to know.

What are environment variables?

Environment variables are values that influence the behavior of applications, processes, and programs running within your Ubuntu system. Often created as name-value pairs, environment variables store system information, configuration files, system-wide settings, user preferences, and other useful data that may be required by applications for their execution.

If you’re into coding, chances are you may have already used environment variables. The PATH variable, which describes the location of an application, is one of the most well-known environment variables that you must configure when setting up the Java path or configuring ADB and Fastboot to run from any directory on your PC.

How to add environment variables in Ubuntu

How to add temporary environment variables

The terminal can be used on Ubuntu to execute powerful commands, including commands to set up custom environment variables. To add a local environment variable that will only remain active until the end of the session and test the process out, follow this guide:

  1. Open the Terminal app using Ctrl+Alt+T.
  2. Use the export command to add the environment variable:
    export my_env_var="val"

    Where my_env_var is the name of the variable and val is the value assigned to it.

    Use the export command to create a temporary environment variable

  3. You can verify whether the variable was added to Ubuntu by entering this command:
    printenv my_env_var
    Verify whether the environment variable has been set by using the printenv command

How to add global environment variables?

Alternatively, you might want to create a global environment variable that permanently remains on your system and works across all user profiles. In this case, you’ll need to add the variable to the system-wide environment config file.

  1. Open the environment file by typing this command in the terminal:
    sudo nano /etc/environment
    Open the environment file on Ubuntu

  2. Enter the environment variable in the following format:
    my_env_var="val"

    Since this file will only accept the name-key value pair, you don’t need to use the export command for the environment file.

    Add the environment variable at the end of the environment file

  3. Press Ctrl+X followed by Y to save and exit the file.

How to add environment variables for the current user?

Although most users tend to utilize global environment variables that store values for all users, Ubuntu also lets you configure user-specific variables. These can come in handy when you wish to create an environment variable that’s restricted to the current user.

  1. Execute this command within the terminal to enter the bashrc file.
    sudo nano ~/.bashrc
    Open the bashrc file

  2. Scroll to the end of this file and type the following code:
    export my_env_var="val"
    Add the environment variable at the end of the bashrc file

  3. Exit bashrc by pressing Ctrl+X and typing Y when prompted to save the changes.

How to delete environment variables in Ubuntu?

Finally, there are times when you may want to delete some previously-set environment variables from Ubuntu. To do so,

  1. From the terminal app, type the unset command followed by the variable name to clear its value from Ubuntu.
    unset my_env_var
    Remove the environment variable with the unset command

  2. (Optional) If you wish to remove an environment variable set in the environment/batchrc files, you’ll have to open them and manually delete the string containing the variable name-value pair.

Wrapping up the setup procedure for environment variables in Ubuntu

That was everything you needed to know to make full use of environment variables in Ubuntu. Besides the popular PATH variable, there are tons of other useful variables like PWD, LOGNAME, and USER that you can use to customize your apps and coding experience.

Speaking of coding, feel free to check out our article on installing Java on Ubuntu if you plan to use the distro for web development. We also have detailed guides on how to set up IDEs like VS Code and Python development environments like Anaconda that you can browse if you encounter issues while installing them on Ubuntu.

NVM on Ubuntu

Related

How to install Node.js on Linux

Node.js is critical to install on Linux if you’re developing using JavaScript. Here’s how to use it on Linux

Partager cet article

Inscrivez-vous à notre newsletter