Emacs 27.1 on Ubuntu 20.04

mkdir $HOME/.local/src
cd $HOME/.local/src
git clone -b master git://git.sv.gnu.org/emacs.git
cd emacs
sudo add-apt-repository ppa:ubuntu-toolchain-r/ppa
sudo apt update
sudo apt build-dep emacs
sudo apt install gcc-10 libgccjit0
sudo apt install libjansson4 libjansson-dev
sudo apt install --no-install-recommends apt-transport-https ca-certificates build-essential autoconf pkg-config git
sudo apt install stow
export CC="gcc-10"
./configure --with-imagemagick --with-xwidgets --with-mailutils --prefix=/usr/local/stow/emacs
sudo make install
cd /usr/local/stow
sudo stow emacs

Emacs 28.1 on Ubuntu 22.04

With native-comp!

git clone git://git.savannah.gnu.org/emacs.git
cd emacs
# Or download a specific version
sudo apt build-dep emacs
# Check "source code" in application manager thingy
sudo apt install libgccjit0 libgccjit-10-dev libjansson4 libjansson-dev
export CC=/usr/bin/gcc-10 && export CXX=/usr/bin/gcc-10
./configure --with-mailutils --with-native-compilation --prefix=/usr/local/stow/emacs
make -j$(nproc)
./src/emacs -Q
# Check if this works
sudo make install
cd /usr/local/stow
sudo stow emacs
# You could also specify a version above if you wanted to install multiple 🤔