How to set environment variables in Ubuntu

With Linux-based laptops becoming increasingly popular, now is the best time to dive straight into the open source operating system if you're interested in programming. However, switching to a Linux distribution can be quite unsettling as it does things very differently than Windows or macOS. For example, the process for setting up environment variables on Linux is much less intuitive than on Windows 11. That's why we've put together this guide to help you configure environment variables on your Ubuntu machine.



Related

What is Ubuntu? The peculiarities of one of the most popular Linux distributions

When it comes to Linux, Ubuntu is one of the most widely used implementations. Here you will find everything you need to know.

What are environment variables?

Environment variables are values ​​that affect the behavior of applications, processes and programs running in 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 applications may need to run.

If you are interested in coding, chances are you 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 need to 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 run powerful commands, including commands to set up custom environment variables. To add a local environment variable that only remains active until the end of the session and test the process, follow these instructions:

  1. Open the Terminal app with Ctrl+Alt+T.
  2. Use that 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 check if the variable has been added to Ubuntu by entering this command:
    printenv my_env_var
    Verify that the environment variable is set using the printenv command

How do I add global environment variables?

Alternatively, you may want to create a global environment variable that remains persistent on your system and works for all user profiles. In this case, you need to add the variable to the system-wide environment configuration 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 only accepts the name-key-value pair, you do not need to use the export command on the environment file.

    Add the environment variable at the end of the environment file

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

How do I add environment variables for the current user?

Although most users tend to use global environment variables that store values ​​for all users, Ubuntu also allows you to configure user-specific variables. These can be useful if you want to create an environment variable that is restricted to the current user.

  1. Run this command in Terminal to enter the bashrc file.
    sudo nano ~/.bashrc
    Open the bashrc file

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

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


How do I delete environment variables in Ubuntu?

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

  1. In the Terminal app, enter the unset command followed by the name of the variable to delete its value from Ubuntu.
    unset my_env_var
    Remove the environment variable using the unset command

  2. (Optional) If you want to remove an environment variable that is set in the environment/batchrc files, you must open them and manually delete the string containing the variable's name-value pair.

Completing the environment variable setup process in Ubuntu

That was everything you needed to know to fully utilize environment variables in Ubuntu. In addition to 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 distribution for web development. We also have detailed guides on setting up IDEs like VS Code and Python development environments like Anaconda that you can browse if you run into problems installing on Ubuntu.

Related

How to install Node.js on Linux

Installing Node.js on Linux is crucial when developing with JavaScript. Here's how to use it on Linux

Leave a Comment